package halma;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class EasyAi {
	Game game;
	ChessBoard chessBoard;
	ChessBoardUnit[][] chessBoardUnits;
	Stack<Coordinate>[] selections = new Stack[10];
	Stack<Coordinate>[] tempSelections = new Stack[10];
	Stack<Coordinate>[] bins = new Stack[10];
	// ÿһ���������
	Coordinate[] ballCoordinates = new Coordinate[10];
	int type;

	public int getType() {
		// System.out.println("getType():"+type);
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	EasyAi(int type, Game game) {
		this.game = game;
		this.chessBoard = game.getChessBoard();
		this.chessBoardUnits = chessBoard.getChessBoardUnits();
		this.type = type;
		for (int i = 0; i < 10; i++) {
			selections[i] = new Stack<Coordinate>();
			tempSelections[i] = new Stack<Coordinate>();
			bins[i] = new Stack<Coordinate>();
		}
		for (int i = 0; i < 17; i++)
			for (int j = 0; j < 17; j++) {
				if (chessBoardUnits[i][j] != null && chessBoardUnits[i][j].getBall() != null
						&& chessBoardUnits[i][j].getBall().getType() == type) {
					ballCoordinates[chessBoardUnits[i][j].getBall().getNumber()] = new Coordinate(i, j, 0);
				}
			}
	}

	public ChessBoard getChessBoard() {
		return chessBoard;
	}

	public void setChessBoard(ChessBoard chessBoard) {
		this.chessBoard = chessBoard;
	}

	// ��������number��ball�����ߵ�·��
	public void check(int number) {
		Coordinate tempC = ballCoordinates[number];
		Stack<Coordinate> tempSelection = tempSelections[number];
		Stack<Coordinate> selection = selections[number];
		Stack<Coordinate> bin = bins[number];
		tempSelection.clear();
		selection.clear();
		bin.clear();
		boolean flag;
		int x, y;
		tempSelection.push(tempC);
		bin.push(tempC);
		 System.out.println("tempC�����꣺"+tempC.getX()+","+tempC.getY());
		int num;// ��¼�ڵ�����
		while (!tempSelection.empty()) {
			System.out.println("temp:"+tempSelection.size());

			
			num = 0;
			System.out.println("ȡ��ǰtempSelection:" + tempSelection.size());
			tempC = tempSelection.pop();
			x = tempC.getX();
			y = tempC.getY();
			System.out.println("ȡ����tempSelection:" + tempSelection.size());
			System.out.println("��tempSelection��ȡ�������꣺" + tempC.getX() + "," + tempC.getY());
			for (int i = -1; i < 2; i++) {
				for (int j = -1; j < 2; j++) {
					if ((i == 0 && j == 0) || (i + j == 0))
						continue;

					// System.out.println("�������꣺"+i+","+j);
					Coordinate sonC = new Coordinate(x + i, y + j, 2);
					//System.out.println("sonC���꣺" + sonC.getX() + "," + sonC.getY());
					if (canMove(tempC, sonC)) {
						// ���ڵ��Ƿ��ѱ���
						flag = false;
						for (int k = bin.size() - 1; k >= 0; k--) {
							Coordinate c = bin.get(k);
							flag = sonC.isEquals(c);
							if (flag) {
								System.out.println("��bin���Ѵ��ڡ�");
								break;

							}
						}
						if (flag)
							continue;
						selection.push(sonC);
						System.out.println("��һ��selection:" + selection.size());

						System.out.println("��һ��push��selection��sonC���꣺" + sonC.getX() + "," + sonC.getY());
						bin.push(sonC);
						System.out.println("��һ��bin:" + bin.size());

						num++;
					} // ����ܷ���Ϊ�½ڵ�
				}
			} // �����һ��Ľڵ�
			for (int i = -2; i < 3; i += 2) {
				for (int j = -2; j < 3; j += 2) {

					if ((i == 0 && j == 0) || (i + j == 0))
						continue;

					// System.out.println("�������꣺"+i+","+j);
					Coordinate sonC = new Coordinate(x + i, y + j);
					if (canMove(tempC, sonC)) {
						// ���ڵ��Ƿ��ѱ���
						flag = false;
						for (int k = bin.size() - 1; k >= 0; k--) {
							Coordinate c = bin.get(k);
							flag = sonC.isEquals(c);
							if (flag) {
								System.out.println("��bin���Ѵ��ڡ�");
								break;
							}
						}
						if (flag)
							continue;
						
						tempSelection.push(sonC);
						System.out.println("-----������tempSelection:" + tempSelection.size());
						System.out.println("sonC:"+sonC.getX()+","+sonC.getY());
						bin.push(sonC);
						System.out.println("������bin:" + bin.size());

						num++;
						System.out.println("num:" + num);
					} // ����ܷ���Ϊ�½ڵ�
				}
			} // ���������Ľڵ�
			if (num == 0) {
				tempC.setType(2);
				selection.push(tempC);
				//System.out.println("������selection:" + selection.size());

			}
			System.out.println("temp:"+tempSelection.size());
		}
	}

	// ��������
	void checkAll() {
		for (int i = 0; i < 10; i++)
			check(i);
	}

	public Coordinate[] select() {
		int num = 0;
		Coordinate[] cArry=new Coordinate[2];
		for (int i = 1; i < 10; i++) {
			if (bins[num].size() < bins[i].size())
				num = i;
		}

		cArry[0]=new Coordinate(ballCoordinates[num].getX(), ballCoordinates[num].getY(),0);
		cArry[1]=new Coordinate(selections[num].peek().getX(), selections[num].peek().getY(),2);


		System.out.println("��㣺" + ballCoordinates[num].getX() + "," + ballCoordinates[num].getY() + "    �յ㣺"
				+ selections[num].peek().getX() + "," + selections[num].peek().getY());

		
		ballCoordinates[num].setX(selections[num].peek().getX());
		ballCoordinates[num].setY(selections[num].peek().getY());
		chessBoardUnits[selections[num].peek().getX()][selections[num].peek().getY()].setBall(chessBoardUnits[ballCoordinates[num].getX()][ballCoordinates[num].getY()].getBall());
		chessBoardUnits[ballCoordinates[num].getX()][ballCoordinates[num].getY()].setBall(null);
	
		

		return cArry;
	}

	// �ж��Ƿ����ƶ�
	public boolean canMove(Coordinate start, Coordinate end) {
		int startX = start.getX(), startY = start.getY(), endX = end.getX(), endY = end.getY();
		if (chessBoardUnits[startX][startY] == null || chessBoardUnits[endX][endY] == null) {
			// System.out.println("��񲻴���");
			return false;
		}
		if (chessBoardUnits[startX][startY].getBall() == null || chessBoardUnits[endX][endY].getBall() != null) {
			// System.out.println("��������ӻ��յ�������");
			return false;
		} else {
			int diffX = endX - startX;
			int diffY = endY - startY;
			// ��һ������
			if ((Math.abs(diffX) == 1 || diffX == 0) && (Math.abs(diffY) == 1 || diffY == 0)) {
				// System.out.println("��һ�񽻻��ɹ�");
				// chessBoardUnits[endX][endY].setBallType(chessBoardUnits[startX][startY].getBallType());
				// chessBoardUnits[startX][startY].setBallType(0);
				return true;
			}
			// ����������
			else if ((Math.abs(diffX) == 2 || diffX == 0) && (Math.abs(diffY) == 2 || diffY == 0)) {
				if (chessBoardUnits[(endX+startX)/2][(startY+endY) / 2] == null
						|| chessBoardUnits[(endX+startX)/2][(startY+endY)/ 2].getBall() == null) {
					// System.out.println("ȱ����Ծ������");
					return false;
				} else {
					// chessBoardUnits[endX][endY].setBallType(chessBoardUnits[startX][startY].getBallType());
					// chessBoardUnits[startX][startY].setBallType(0);
					// System.out.println("������ɹ�");
					return true;
				}
			}
			// �޷��ߵ����
			else {
				// System.out.println("�������Ӳ���һ��ֱ����");
				return false;
			}
		}
	}

}
